from vec import Vec2
import pgzrun
WIDTH = 500
HEIGHT = 500

PEN_COLOR = (0, 200, 0)
BACK_COLOR = (0, 0, 0)
ptList = []
length = 200
pt_mid = Vec2(WIDTH / 2, HEIGHT / 2)
pt_a = Vec2(pt_mid.x-length//2, pt_mid.y -length//2)
pt_b = Vec2(pt_mid.x+length//2, pt_mid.y -length//2)
pt_c = Vec2(pt_mid.x+length//2, pt_mid.y +length//2)
pt_d = Vec2(pt_mid.x-length//2, pt_mid.y +length//2)

ptList.append(pt_a)
ptList.append(pt_b)
ptList.append(pt_c)
ptList.append(pt_d)

def fractal(ptList):
    i = 0
    while True:
        if i == len(ptList) - 1:
            line_fractal(ptList, i, 0)            
            break
        else:
            i = line_fractal(ptList, i, i + 1)

def get_point(a,b):
    a.x*=b.x
    a.y*=b.y
    return a

def line_fractal(ptList,a,b):
    dir=ptList[b]-ptList[a]
    l=dir.length()/4
    dir.normalize()
    v1=Vec2(dir.y,-dir.x)
    v2=Vec2(-v1.y,v1.x)
    v3=Vec2(-v1.x,-v1.y)
    pt1=ptList[a]*3/4+ptList[b]*1/4
    pt2=pt1+v1*l  
    pt3=pt2+v2*l    
    pt4=pt3+v3*l
    pt5=pt4+v3*l
    pt6=pt5+v2*l
    pt7=pt6+v1*l
    ptList.insert(a + 1, pt7)
    ptList.insert(a + 1, pt6)
    ptList.insert(a + 1, pt5)
    ptList.insert(a + 1, pt4)
    ptList.insert(a + 1, pt3)
    ptList.insert(a + 1, pt2)
    ptList.insert(a + 1, pt1)
    return a + 8
    
for i in range(2):
    fractal(ptList)

def draw():
    screen.fill(BACK_COLOR)
    for i in range(len(ptList)):
        if i == len(ptList) - 1:
            screen.draw.line(ptList[i].raw_get(), ptList[0].raw_get(), PEN_COLOR)            
        else:
            screen.draw.line(ptList[i].raw_get(), ptList[i + 1].raw_get(), PEN_COLOR)    

pgzrun.go()